import json
import unittest
import sys
sys.path.append("../main/")
from JsonParse import JSONParser

class TestJSONParsing(unittest.TestCase):

    def setUp(self):
        fp = open("../../json/jira.json", "r")
        self.parser = JSONParser(fp.read())
        fp.close()
        fp = open("../../json/jira.json", "r")
        self.data = json.loads(fp.read())
        fp.close()
        self.search = {"fromString": ["changelog", "histories", 4, "items", 0, "fromString"], "toString": ["changelog", "histories", 4, "items", 0, "toString"], "displayName": ["changelog", "histories", 4, "author", "displayName"]}
        self.string = {"expand": "expand"}

    
    def test_getSearch(self):
        search = self.search['displayName']
        table = self.parser.getItems(search, self.data)
        #print table
        self.assertTrue(table == [[u'Andrew Hammett'], [u'Andrew Hammett'], [u'Martin Li'], [u'Andrew Hammett'], [u'Mark Schulze'], [u'Mark Schulze'], [u'Mark Schulze']])
        table = self.parser.getItems(self.search['fromString'], self.data)
        #print table
        self.assertTrue(table == [['None'], ['None'], ['None'], ['None'], ['None'], ['None'], ['None'], ['None'], ['None'], ['None'], ['None'],
                                  ['None'], [u'Ready'], ['None'], [u'2013-01-29 14:47:09.0'], ['None'], ['None'], [u'78958'], [u'In Progress'],
                                  ['None'], ['None']])
        table = self.parser.getItems(["changelog", "histories", 0, ["items", 0, "field"], ["author", "displayName"]], self.data)
        #print table
        #print " "
        #attributes = self.parser.getAttributes(self.data)
        #print attributes
        columns = self.parser.getColumns()
        #print columns
        table = self.parser.getItems(["changelog", "histories", 0, ["author", "displayName"], ["items", 0, "field"]], self.data)
        dataGrid = self.parser.getDataGridStore(["displayName", "field"], table)
        #print dataGrid
        #print table
        """
        self.assertTrue(table == [[u'Andrew Hammett', u'Attachment'], [u'Andrew Hammett', u'Attachment'], [u'Andrew Hammett', u'Attachment'],
                                  [u'Andrewammett', u'Attachment'], [u'Andrew Hammett', u'Attachment'], [u'Andrew Hammett', u'Attachment'],
                                  [u'Martin Li', u'Attacment'], [u'Martin Li', u'Attachment'], [u'Martin Li', u'Attachment'],
                                  [u'Andrew Hammett', u'Attachment'], [u'Andrew Hamett', u'Attachment'], [u'Andrew Hammett', u'Attachment'],
                                  [u'Mark Schulze', u'assignee'], [u'Mark Schulze', u'status'], [u'Mark Schulze', u'WorklogId'],
                                  [u'Mark Schulze', u'timespent'], [u'Mark Schulze', u'timeestimate'], [u'Mark Schulze', u'resolutiondate'],
                                  [u'Mark Schulze', u'resolution'], [u'Mark Schulze', u'Component'], [u'Mark Schulze', u'status']])
        """
        

    """
    I don't really like the fact that when you are only getting on variable it doesn't return a list in a list
    but for now I will let it slide
    """
    def test_getString(self):
        self.expand = self.parser.getString(["expand"], self.data)
        #print self.expand
        self.assertTrue(self.expand == ['renderedFields,names,schema,transitions,operations,editmeta,changelog'])
        self.startAt = self.parser.getString(["changelog", "startAt"], self.data)
        #print self.startAt
        self.assertTrue(self.startAt == [0])
        self.id = self.parser.getString(["changelog", "histories", 0, "id"], self.data)
        #print self.id
        self.assertTrue(self.id == [[u'3513297'], [u'3515577'], [u'3515786'], [u'3515792'], [u'3550852'], [u'3550875'], [u'3550876']])

    def test_getTable(self):
        data = {'search':[["displayName", ["changelog", "histories", 4, "author", "displayName"]],["field", ["changelog", "histories", 0, "items", 0, "field"]]], 'JQL':""}
        #fp = open("../../json/jira.json", "r")
        #self.data = json.loads(fp.read())
        #dataGrid = self.parser.getTable(data['search'], ["changelog", "histories", 0, ["author", "displayName"], ["items", 0, "field"]])
        #table = self.parser.getItems(["changelog", "histories", 0, ["author", "displayName"], ["items", 0, "field"]], self.data)
        #dataGrid = self.parser.getDataGridStore(["displayName", "field"], table)
        #print json.dumps(dataGrid)


    def test_convertPaths(self):
        paths = self.parser.convertPaths([[u'changelog', u'histories', u'0', u'items', u'0', u'toString'], [u'changelog', u'histories', u'0', u'items', u'0', u'fieldtype'],
         [u'changelog', u'histories', u'0', u'created'], [u'changelog', u'histories', u'0', u'id'], [u'changelog', u'histories', u'0', u'author', u'displayName'],
         [u'changelog', u'histories', u'0', u'author', u'name'], [u'changelog', u'histories', u'0', u'author', u'self'],
         [u'changelog', u'histories', u'0', u'author', u'avatarUrls', u'48x48'], [u'changelog', u'histories', u'0', u'author', u'avatarUrls', u'16x16']])
        print paths
        columns = [[u'changelog', u'histories', u'0', u'created'], [u'changelog', u'histories', u'0', u'id'], [u'changelog', u'histories', u'0', u'author', u'displayName'],
         [u'changelog', u'histories', u'0', u'author', u'name'], [u'changelog', u'histories', u'0', u'author', u'self'],
         [u'changelog', u'histories', u'0', u'author', u'avatarUrls', u'48x48'], [u'changelog', u'histories', u'0', u'author', u'avatarUrls', u'16x16'],
         [u'changelog', u'histories', u'0', u'author', u'emailAddress'], [u'changelog', u'histories', u'0', u'author', u'active'], [u'changelog', u'maxResults']]
        print self.parser.getTable(columns, paths)
        test = [u'changelog', 'histories', '0', ['items', '0', ['toString'], ['fieldtype']], ['author', ['self'], ['avatarUrls',
                                                                                                            ['48x48'], ['16x16']], ['displayName'], ['name']], ['id'], ['created']]
        paths = self.parser.convertPaths([["changelog", "histories", 0, "items", 0, "field"], ["changelog", "histories", 0, "items", 0, "fieldtype"]])
        #print paths
        #self.assertTrue(paths == ["changelog", "histories", 0, "items", 0, ["field"], ["fieldtype"]])
        paths = self.parser.convertPaths([["changelog", "histories", 0, "items", 4, "field"], ["changelog", "histories", 0, "items", 0, "fieldtype"]])
        #print paths
        paths = self.parser.convertPaths([["changelog", "field"], ["changelog", "histories", 0, "items", 0, "fieldtype"]])
        #print paths
        paths = self.parser.convertPaths([["changelog", "field"], ["changelog", "histories", 0, "items", 0, "fieldtype"], ["changelog", "test", "field"]])
        #print paths
    """
    def test_sample(self):
        with self.assertRaises(ValueError):
            random.sample(self.seq, 20)
        for element in random.sample(self.seq, 5):
            self.assertTrue(element in self.seq)
    """

if __name__ == '__main__':
    unittest.main()
